package com.bonc.ioc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.StatisticsDao;
import com.bonc.ioc.model.DataAssetInfo;
import com.bonc.ioc.model.FlowRecordInfo;
import com.bonc.ioc.model.StatisticsInfo;
import com.bonc.ioc.service.IStatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author ZhangXu
 * @date 2021/3/31 15:13
 */
@Service
public class StatisticsServiceImpl extends BaseService implements IStatisticsService {
    private static final Logger log = LoggerFactory.getLogger(StatisticsServiceImpl.class);

    @Resource
    private StatisticsDao statisticsDao;

    @Override
    public JSONObject queryDataSources() {
        JSONObject result = new JSONObject();
        List<StatisticsInfo> list = statisticsDao.queryDataSources();
        log.info("统计 数据来源 查询结果={}", JSON.toJSONString(list));
        int total = list.stream().mapToInt(StatisticsInfo::getTotal).sum();
        for (StatisticsInfo statistics : list) {
            double percentage = BigDecimal.valueOf(statistics.getTotal() * 100 / total).setScale(2, BigDecimal.ROUND_UP).doubleValue();
            statistics.setPercentage(percentage);
        }
        log.info("统计 数据来源 处理数据={}", JSON.toJSONString(list));
        result.put("DataSources", list);
        return result;
    }

    @Override
    public JSONObject querySubjects() {
        JSONObject result = new JSONObject();
        String dicType = "10";
        List<StatisticsInfo> list = statisticsDao.querySubjects(dicType);
        log.info("统计 学科分类 查询结果={}", JSON.toJSONString(list));
        for (StatisticsInfo statisticsInfo : list) {
            setCategoryPercentage(statisticsInfo);
        }
        log.info("统计 学科分类 处理数据={}", JSON.toJSONString(list));
        result.put("Subjects", list);
        return result;
    }

    @Override
    public JSONObject queryAnnualIncrement() {
        JSONObject result = new JSONObject();
        Map<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("TotalNum", getIncrementData(1,3));
        resultMap.put("TotalSize", getIncrementData(2,3));
        resultMap.put("TotalHits", getIncrementData(3,3));
        resultMap.put("TotalUse", getIncrementData(4,3));
        result.put("AnnualIncrement", resultMap);
        return result;
    }

    @Override
    public JSONObject queryMonthlyIncrement() {
        JSONObject result = new JSONObject();
        Map<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("TotalNum", getIncrementData(1,2));
        resultMap.put("TotalSize", getIncrementData(2,2));
        resultMap.put("TotalHits", getIncrementData(3,2));
        resultMap.put("TotalUse", getIncrementData(4,2));
        result.put("MonthlyIncrement", resultMap);
        return result;
    }

    @Override
    public JSONObject queryResourcesTotal() {
        JSONObject result = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        int item = 2;
        int type = 1;
        List<FlowRecordInfo> flowRecordInfo = statisticsDao.queryTotalFlowRecord(item, type);
        result.put("ResourcesTotal", flowRecordInfo);
        return result;
    }

    @Override
    public JSONObject queryDocumentsTotal() {
        JSONObject result = new JSONObject();
        int item = 1;
        int type = 1;
        List<FlowRecordInfo> flowRecordInfo = statisticsDao.queryTotalFlowRecord(item, type);
        result.put("DocumentsTotal", flowRecordInfo);
        return result;
    }

    @Override
    public JSONObject queryCommonData() {
        JSONObject result = new JSONObject();
        int type = 2;
        List<DataAssetInfo> flowRecordInfo = statisticsDao.queryDataAssetRanking(type);
        result.put("CommonData", flowRecordInfo);
        return result;
    }

    @Override
    public JSONObject queryMonthlyDownloads() {
        JSONObject result = new JSONObject();
        int item = 4;
        int type = 2;
        List<FlowRecordInfo> flowRecordInfo = statisticsDao.queryTotalFlowRecord(item, type);
        result.put("MonthlyDownloads", flowRecordInfo);
        return result;
    }

    @Override
    public JSONObject queryDataLatestReleasedRanking() {
        JSONObject result = new JSONObject();
        Integer type = 1;
        List<DataAssetInfo> flowRecordInfo = statisticsDao.queryDataAssetRanking(type);
        result.put("DataLatestReleasedRanking", flowRecordInfo);
        return result;
    }

    @Override
    public JSONObject queryDataDownloadsRanking() {
        JSONObject result = new JSONObject();
        Integer type = 2;
        List<DataAssetInfo> flowRecordInfo = statisticsDao.queryDataAssetRanking(type);
        result.put("DataDownloadsRanking", flowRecordInfo);
        return result;
    }

    @Override
    public JSONObject queryDataCollectionRanking() {
        JSONObject result = new JSONObject();
        Integer type = 3;
        List<DataAssetInfo> flowRecordInfo = statisticsDao.queryDataAssetRanking(type);
        result.put("DataCollectionRanking", flowRecordInfo);
        return result;
    }

    @Override
    public JSONObject queryDataViewsRanking() {
        JSONObject result = new JSONObject();
        Integer type = 4;
        List<DataAssetInfo> flowRecordInfo = statisticsDao.queryDataAssetRanking(type);
        result.put("DataViewsRanking", flowRecordInfo);
        return result;
    }

    /**
     * 添加 数据所占百分比
     * @param statisticsInfo
     */
    private void setCategoryPercentage(StatisticsInfo statisticsInfo) {
        long total = statisticsInfo.getTotal();
        double num = statisticsInfo.getTotal();
        if ("10".equals(statisticsInfo.getDicType())){
            statisticsInfo.setPercentage(100.00);
        }
        if (statisticsInfo.getDicChildList().size() < 1 || total == 0 ) {
            return;
        }
        for (StatisticsInfo child : statisticsInfo.getDicChildList()) {
            setCategoryPercentage(child);
            num = child.getTotal();
            double percentage = BigDecimal.valueOf((num / total) * 100).setScale(2, BigDecimal.ROUND_UP).doubleValue();
            child.setPercentage(percentage);
        }
    }

    /**
     * 获取增量数据
     * @param item 数据类型 1:总数量, 2:总容量, 3:访问量, 4:使用量
     * @param type 记录类型 1:日度, 2:月度, 3:年度
     * @return
     */
    private LinkedList<HashMap<String, Object>> getIncrementData(Integer item, Integer type) {
        List<FlowRecordInfo> list = statisticsDao.queryTotalFlowRecord(item, type);
        LinkedList<HashMap<String, Object>> resultList = new LinkedList<>();
        for (int i = 1; i < list.size(); i++) {
            HashMap<String, Object> annualIncrement = new HashMap<>();
            Long increment = list.get(i).getRecordData() - list.get(i - 1).getRecordData();
            annualIncrement.put("data", increment);
            annualIncrement.put("time", list.get(i).getRecordTime());
            resultList.add(annualIncrement);
        }
        return resultList;
    }
}
